package com.xitang.common.service.impl;

import com.xitang.common.bean.BizCategory;
import com.xitang.common.dto.CategoryQueryReq;
import com.xitang.common.dto.PageQueryRsp;
import com.xitang.common.mapper.BizCategoryMapper;
import com.xitang.common.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class CategoryServiceImpl implements ICategoryService {
    @Autowired
    private BizCategoryMapper categoryMapper;

    @Override
    public PageQueryRsp<BizCategory> getCategoryList(CategoryQueryReq queryReq) {
        Long totalCount = categoryMapper.selectCountByCustom(queryReq.getCategoryCode(), queryReq.getCategoryName());
        if (Objects.isNull(totalCount) || totalCount < 1) {
            return new PageQueryRsp<>();
        }
        int pageNo = queryReq.getPageNo();
        int pageSize = queryReq.getPageSize();
        int offset = (pageNo - 1) * pageSize;
        List<BizCategory> bizCategories = categoryMapper.selectDataByCustom(offset, pageSize, queryReq.getCategoryCode(),
                queryReq.getCategoryName());
        PageQueryRsp<BizCategory> pageQueryRsp = new PageQueryRsp<>();
        pageQueryRsp.setRecords(bizCategories);
        pageQueryRsp.setTotal(totalCount);
        return pageQueryRsp;
    }

    @Override
    public BizCategory getCategoryDetail(String categoryCode) {
        return categoryMapper.selectById(categoryCode);
    }

    @Override
    public void addCategory(BizCategory category) {
        categoryMapper.insert(category);
    }

    @Override
    public void updateCategoryDetail(BizCategory category) {
        categoryMapper.updateById(category);
    }
}
